package com.example.rentsystem.servlet.pages;

import com.example.rentsystem.entity.User;
import com.example.rentsystem.service.HouseService;
import com.example.rentsystem.service.UserService;
import com.example.rentsystem.service.impl.HouseServiceImpl;
import com.example.rentsystem.service.impl.UserServiceImpl;
import com.example.rentsystem.utils.ThymeleafUtil;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/personal-info")
public class PersonalInfoServlet extends HttpServlet {

    private UserService userService;
    private HouseService houseService;

    public PersonalInfoServlet() {
        userService = new UserServiceImpl();
        houseService = new HouseServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getSession().setAttribute("login-first", true);
            resp.sendRedirect("login");
            return;
        }
        Context context = new Context();
        String id = req.getParameter("id");
        if (id != null && !id.equals("")) {
            context.setVariable("u", userService.getUserById(Integer.parseInt(id)));
            context.setVariable("houses", houseService.getUserHouses(Integer.parseInt(id)));
        } else context.setVariable("u", user);
        context.setVariable("user", user);

        String modifyInfo = "";
        String modifyInfoStyle = "";
        if (session.getAttribute("username-exist") != null) {
            modifyInfo = "修改失败, 可能原因: (用户名重复)";
            modifyInfoStyle = "color: red;font-weight: bold;";
            session.removeAttribute("username-exist");
        } else if (session.getAttribute("account-exist") != null) {
            modifyInfo = "修改失败, 可能原因: (账号重复)";
            modifyInfoStyle = "color: red;font-weight: bold;";
            session.removeAttribute("account-exist");
        } else if (session.getAttribute("invalid-phone") != null) {
            modifyInfo = "修改失败, 请输入正确的手机号码";
            modifyInfoStyle = "color: red;font-weight: bold;";
            session.removeAttribute("invalid-phone");
        } else if (session.getAttribute("name-short") != null) {
            modifyInfo = "修改失败, 名字长度应该在2到6位之间";
            modifyInfoStyle = "color: red;font-weight: bold;";
            session.removeAttribute("name-short");
        }
        String a = "", aStyle = "";
        if (session.getAttribute("password-short") != null) {
            a = "修改失败, 可能原因: (密码长度应该在8到18位)";
            aStyle = "color: red;font-weight: bold;";
            session.removeAttribute("password-short");
        } else if (session.getAttribute("password-not-equal") != null) {
            a = "修改失败, 可能原因: (两次密码输入不正确)";
            aStyle = "color: red;font-weight: bold;";
            session.removeAttribute("password-not-equal");
        }
        context.setVariable("modify_info", modifyInfo);
        context.setVariable("modify_info_style", modifyInfoStyle);
        context.setVariable("a", a);
        context.setVariable("aStyle", aStyle);
        ThymeleafUtil.process("personal-info.html", context, resp.getWriter());
    }
}
